package com.sale.taskplugins;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.member.common.DateUtil;
import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【到期发行活动开始】
 */
public class StartActivityTaskPluginImpl implements PluginConnector, TimeTaskService {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public boolean run() {
        exeStartActivityTask();
        return true;
    }

    @Override
    public Object execute(ParamsVo arg0) throws Exception {
        exeStartActivityTask();
        return new ResultEntity();
    }

    @SuppressWarnings("rawtypes")
    public void exeStartActivityTask() {
        log.info("定时任务-----》开启发行活动启动");
        List<DataMap> dataList = getSaleInfo();
        int size = dataList.size();
        log.info("当日需开启发行的活动数量为" + size);
        if (size > 0) {
            log.info("当日需开启发行的活动数量为" + size);
            Map<String, Object> params = new HashMap<String, Object>();
            for (DataMap dataMap : dataList) {
                String saleId = String.valueOf(dataMap.get("saleId"));//发行活动ID
                String saleName = String.valueOf(dataMap.get("saleName"));
                log.info(saleName + "：======》开启发行活动");
                params.put("trsSale.w_saleId", saleId);
                //开启发行活动，状态设为30
                params.put("trsSale.saleStatus", "30");
                params.put("modelTypes", "u_trsSale");
                //更新状态
                updateSaleInfo(params);
                log.info(saleName + "：======》开启完成");
            }
        }
    }

    /**
     * 综合查询：查询当日为发行开始日期的所有申购活动sale对象
     *
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<DataMap> getSaleInfo() {
        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                .getBean("netProxyManager");
        ParamsVo params = new ParamsVo();
        params.setParams("sqlKeyId", "ByObjInfo");
        params.setKey("publicForSql");
        String now = DateUtil.getNowTimeWithOutMinutes();
        StringBuffer dyncSql = new StringBuffer(
                "select s.sale_id saleId,s.sale_name saleName from trs_sale s");
        dyncSql.append(" where s.sale_status='20'");
        dyncSql.append(" and s.sale_date_start='" + now + "'");
        params.setParams("dyncSql", dyncSql.toString());
        log.info("自动结束发行，查询活动信息、申购总数SQL=>" + dyncSql);
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        List<DataMap> list = (List) dymicSqlParamResult.getEntity();
        return list;
    }

    /**
     * 修改配号表信息，更新中签状态
     */
    @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
    public Object updateSaleInfo(Map<String, Object> saleMap) {
        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                .getBean("netProxyManager");
        ParamsVo params = new ParamsVo();
        params.setParams(saleMap);
        params.setParams("doCode", "dymicSql");
        params.setParams("sqlKeyId", "ByDymicSql");
        params.setParams("resultType", "processSql");
        params.setKey("publicForSql");
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        Map result = (Map) dymicSqlParamResult.getEntity();
        return result;
    }
}
